<template>
	<view class="all">
		<view class="tupian">
			<view class="txt">
				<text class="text1">请上传歌单的封面</text>
			</view>
			<view class="up">
			<u-upload :fileList="fileList1" @afterRead="afterRead" @delete="deletePic" name="1" multiple :maxCount="10"
				height="300rpx" width="300rpx" uploadIconColor="#000000" style="background-color: #f0f0f0;" >
			</u-upload>
			</view>
		</view>
		<view>
			<button type="primary" class="bu" @tap="tijiao">更新头像</button>
		</view>
	</view>
</template>

<script>
	import {
		mapState,
		mapMutations
	} from 'vuex'
	export default {
		data() {
			return {
				fileList1:[],
				listPhoto:""
			};
		},
		computed: {
			...mapState("user", ['userInfo'])
		},
		methods:{
			...mapMutations('user', ['updateUserInfo']),
			// 删除图片
			deletePic(event) {
				this[`fileList${event.name}`].splice(event.index, 1)
			},
			// 新增图片
			async afterRead(event) {
				// 当设置 multiple 为 true 时, file 为数组格式，否则为对象格式
				let lists = [].concat(event.file)
				let fileListLen = this[`fileList${event.name}`].length
				lists.map((item) => {
					this[`fileList${event.name}`].push({
						...item,
						status: 'uploading',
						message: '上传中'
					})
				})
				for (let i = 0; i < lists.length; i++) {
					const result = await this.uploadFilePromise(lists[i].url)
					let item = this[`fileList${event.name}`][fileListLen]
					this[`fileList${event.name}`].splice(fileListLen, 1, Object.assign(item, {
						status: 'success',
						message: '',
						url: result
					}))
					fileListLen++
				}
			},
			uploadFilePromise(url) {
				let that = this
				return new Promise((resolve, reject) => {
					let a = uni.uploadFile({
						url: 'http://124.221.244.228:8089/upload', // 仅为示例，非真实的接口地址
						filePath: url,
						name: 'file',
						formData: {
							user: 'test'
						},
						success: (res) => {
							setTimeout(() => {
								resolve(res.data.data)
							}, 1000)
							let data = JSON.parse(res.data)
							that.listPhoto = data.data
						}
					});
				})
			},
			async tijiao(){
				console.log(this.userInfo.userId);
				const {
					data: res
				} = await uni.$http.get("/user/updatePhoto",{
					userId:this.userInfo.userId,
					userPhoto:this.listPhoto
				});
				console.log(res);
				const {
					data:res1
				}=await uni.$http.get("/user/findUserById",{
					userId:this.userInfo.userId,
				})
				this.updateUserInfo(res1.data)
				setTimeout(()=>{
					uni.navigateBack()
				},1000)
				uni.showToast({
					duration:1000,
					title:"请下拉刷新页面",
					position:'center',
					icon:"success"
				})
			}
		}
	}
</script>

<style lang="scss">
	.all{
		height: 100vh;
		position: relative;
		width: 100%;
	}
	.bu{
		position: absolute;
		bottom: 100rpx;
		left: 50%;
		transform: translateX(-200rpx);
		width: 400rpx;
		justify-content: center;
	}
	.tupian{
		position: relative;
		.txt{
			text-align: center;
			font-size: 50rpx;
			font-weight: 600;
			margin-top: 30rpx;
		}
		.up{
			position: absolute;
			left: 50%;
			transform: translateX(-150rpx);
			margin-top: 50rpx;
		}
	}
</style>
